import { _decorator, Component, EventTouch, Input, Node } from 'cc';
import { D_DOWN, D_LEFT, D_RIGHT, D_UP, A_STAND, A_WALK } from './Constants';
import { CharacterController } from './CharacterController';
import { MapController } from './MapController';
const { ccclass, property } = _decorator;

@ccclass('TouchAndMoveV2')
export class TouchAndMoveV2 extends Component {

    characterController:CharacterController;

    mapController:MapController;

    adjX = 0;
    adjY = 0;

    @property
    speed = 0;

    targetX = 0;
    targetY = 0;

    currentX = 0;
    currentY = 0;

    setPlayer(player:Node){
        this.characterController = player.getComponent(CharacterController);
    }

    setMap(map:Node){
        this.mapController = map.getComponent(MapController);
    }

    init() {
        const wp = this.node.getWorldPosition()
        this.currentX = this.targetX;
        this.currentY = this.targetY;
        this.adjX = -wp.x;
        this.adjY = -wp.y;
        this.node.on(Input.EventType.TOUCH_END,(event:EventTouch)=>{
            const loc = event.getUILocation();
            let target = {
                x: loc.x+this.currentX+this.adjX,
                y: loc.y+this.currentY+this.adjY
            }
            target = this.mapController.roundToGrid(target);
            this.targetX = Math.round(target.x)|0;
            this.targetY = Math.round(target.y)|0;
            console.log("WORLD touch end, loc=("+loc.x+","+loc.y+") current=("+this.currentX+","+this.currentY+") adj=("+this.adjX+","+this.adjY+") target=("+this.targetX+","+this.targetY+")")
        })
    }

    count = 0;
    tmpSetActionCost = 0;
    tmpMovePlayerCost = 0;
    tmpMoveMapCost = 0;

    render(dt: number) {
        this.count++;
        let t0 = 0;
        let t1 = 0;
        let dist = dt*this.speed
        const dx = Math.round(this.targetX-this.currentX)|0;
        const dy = Math.round(this.targetY-this.currentY)|0;
        if(dx!=0||dy!=0){
            t0 = performance.now();
            if(dx>0){
                const mv = Math.min(dist,dx);
                this.currentX += mv;
                dist -= mv;
                // console.log("move x "+mv)
                this.characterController.setDirectionAction({direction:D_RIGHT,action:A_WALK});
            }
            else if(dx<0){
                const mv = Math.min(dist,-dx);
                this.currentX -= mv;
                dist -= mv;
                // console.log("move x -"+mv)
                this.characterController.setDirectionAction({direction:D_LEFT,action:A_WALK});
            }
            if(dist>0){
                if(dy>0){
                    const mv = Math.min(dist,dy);
                    this.currentY += mv;
                    // console.log("move y "+mv)
                    this.characterController.setDirectionAction({direction:D_UP,action:A_WALK});
                }
                else if(dy<0){
                    const mv = Math.min(dist,-dy);
                    this.currentY -= mv;
                    // console.log("move y -"+mv)
                    this.characterController.setDirectionAction({direction:D_DOWN,action:A_WALK});
                }
            }
            t1 = performance.now();
            this.tmpSetActionCost += t1-t0;

            t0 = t1;
            this.characterController.moveTo({x:this.currentX,y:this.currentY});
            t1 = performance.now();
            this.tmpMovePlayerCost += t1-t0;

            t0 = t1;
            this.mapController.viewAt({x:this.currentX,y:this.currentY});
            t1 = performance.now();
            this.tmpMoveMapCost += t1-t0;
        }
        else{
            t0 = performance.now();
            this.characterController.setDirectionAction({action:A_STAND});
            t1 = performance.now();
            this.tmpSetActionCost += t1-t0;
        }
        if(this.count%1000===0){
            console.log("set action: "+this.tmpSetActionCost+", move player: "+this.tmpMovePlayerCost+", move map: "+this.tmpMoveMapCost);
        }
    }
}
